package com.ytjj.qmyx.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.constants.RedisKeyConstant;
import com.ytjj.common.dto.mqdto.*;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.data.model.mongo.*;
import com.ytjj.qmyx.data.service.AppActivationService;
//import com.ytjj.qmyx.data.service.KafkaSendService;
import com.ytjj.qmyx.mall.model.DynamicDto;
import com.ytjj.qmyx.mall.model.UserRegisterRequestDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 *
 *  *
 **/
@Slf4j
@Service
public class AppActivationServiceImpl implements AppActivationService {

    @Resource
    private MongoTemplate mongoTemplate;

//    @Autowired
//    private KafkaSendService kafkaSendService;

    @Autowired
    private RedisService redisService;

    /**
     * app激活
     * @param dto
     */
    @Override
    public void appActivation(AppDataStatisticsActivationDto dto) {
        AppDataStatisticsActivation activation = new AppDataStatisticsActivation();
        BeanUtils.copyProperties(dto, activation);
        if (StringUtils.isEmpty(activation.getPlatform())) {
            activation.setPlatform("app");
        }
        if (StringUtils.isEmpty(activation.getChannel())) {
            activation.setChannel("default");
        }
        if (StringUtils.isEmpty(activation.getSeq_id())) {
            activation.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(activation.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            activation.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(activation);
        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.APP_ACTIVATION_KAFKA_OUT_KEY);
        activation.setIncrementId(incrementId);
        activation.setCur_timestamp(System.currentTimeMillis());
        //发送到kafka
//        try{
//            log.info("appActivationDto={}", JSONObject.toJSONString(activation));
//            boolean sendStatus = kafkaSendService.sendAppActivaionMsg(JSONObject.toJSONString(activation));
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

    /**
     * 用户活跃
     * @param dto
     */
    @Override
    public void appUserAtive(AppDataStatisticsUsersActiveDto dto) {
        AppDataStatisticsUsersActive usersActive = new AppDataStatisticsUsersActive();
        BeanUtils.copyProperties(dto, usersActive);
        if (StringUtils.isEmpty(usersActive.getPlatform())) {
            usersActive.setPlatform("app");
        }
        if (StringUtils.isEmpty(usersActive.getChannel())) {
            usersActive.setChannel("default");
        }
        if (StringUtils.isEmpty(usersActive.getSeq_id())) {
            usersActive.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(usersActive.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            usersActive.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(usersActive);

        //发送到kafka
//        try{
//            log.info("usersActive={}", JSONObject.toJSONString(usersActive));
//            boolean sendStatus = kafkaSendService.sendAppUserActiveMsg(JSONObject.toJSONString(usersActive));
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

    /**
     * 用户行为
     * @param dto
     */
    @Override
    public void appUserBehavior(AppDataStatisticsUsersBehaviorDto dto) {
        AppDataStatisticsUsersBehavior usersBehavior = new AppDataStatisticsUsersBehavior();
        BeanUtils.copyProperties(dto, usersBehavior);
        if (StringUtils.isEmpty(usersBehavior.getPlatform())) {
            usersBehavior.setPlatform("app");
        }
        if (StringUtils.isEmpty(usersBehavior.getChannel())) {
            usersBehavior.setChannel("default");
        }
        if (StringUtils.isEmpty(usersBehavior.getSeq_id())) {
            usersBehavior.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(usersBehavior.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            usersBehavior.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(usersBehavior);
        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.APP_USERBEHAVIOR_KAFKA_OUT_KEY);
        usersBehavior.setIncrementId(incrementId);
        usersBehavior.setCur_timestamp(System.currentTimeMillis());
        //发送到kafka
//        try{
//            log.info("usersBehavior={}", JSONObject.toJSONString(usersBehavior));
//            boolean sendStatus = kafkaSendService.sendAppUserBehavior(JSONObject.toJSONString(usersBehavior));
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

    @Override
    public void appUserRegister(UserRegisterRequestDto dto) {
        AppUserRegister userRegister = new AppUserRegister();
        BeanUtils.copyProperties(dto, userRegister);
        if (StringUtils.isEmpty(userRegister.getPlatform())) {
            userRegister.setPlatform("app");
        }
        if (StringUtils.isEmpty(userRegister.getChannel())) {
            userRegister.setChannel("default");
        }
        if (StringUtils.isEmpty(userRegister.getSeq_id())) {
            userRegister.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(userRegister.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            userRegister.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(userRegister);
        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.APP_USERREGISTER_KAFKA_OUT_KEY);
        userRegister.setIncrementId(incrementId);
        userRegister.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.info("usersRegister={}", JSONObject.toJSONString(userRegister));
//            boolean sendStatus = kafkaSendService.sendAppUserRegister(JSONObject.toJSONString(userRegister));
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

    @Override
    public void dynamicCheckPass(DynamicDto dto) {
        AppEvaluationInfo appEvaluationInfo = new AppEvaluationInfo();
        BeanUtils.copyProperties(dto, appEvaluationInfo);
        if (StringUtils.isEmpty(appEvaluationInfo.getPlatform())) {
            appEvaluationInfo.setPlatform("app");
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getChannel())) {
            appEvaluationInfo.setChannel("default");
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getSeq_id())) {
            appEvaluationInfo.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            appEvaluationInfo.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(appEvaluationInfo);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.DYNAMIC_CHECK_PASS_KAFKA_OUT_KEY);
        appEvaluationInfo.setIncrementId(incrementId);
        appEvaluationInfo.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.info("appEvaluationAdd={}", JSONObject.toJSONString(appEvaluationInfo));
//            boolean sendStatus = kafkaSendService.sendDynamicCheckPass(JSONObject.toJSONString(appEvaluationInfo));
//        }catch (Exception e){
//            log.error("dynamicCheckPass发送到kafka错误",e);
//        }
    }

    @Override
    public void appEvaluationAdd(DynamicDto dto) {
        AppEvaluationInfo appEvaluationInfo = new AppEvaluationInfo();
        BeanUtils.copyProperties(dto, appEvaluationInfo);
        if (StringUtils.isEmpty(appEvaluationInfo.getPlatform())) {
            appEvaluationInfo.setPlatform("app");
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getChannel())) {
            appEvaluationInfo.setChannel("default");
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getSeq_id())) {
            appEvaluationInfo.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(appEvaluationInfo.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            appEvaluationInfo.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(appEvaluationInfo);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.DYNAMIC_CHECK_PASS_KAFKA_OUT_KEY);
        appEvaluationInfo.setIncrementId(incrementId);
        appEvaluationInfo.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.info("appEvaluationAdd={}", JSONObject.toJSONString(appEvaluationInfo));
//            boolean sendStatus = kafkaSendService.sendDynamicCheckPass(JSONObject.toJSONString(appEvaluationInfo));
//        }catch (Exception e){
//            log.error("dynamicCheckPass发送到kafka错误",e);
//        }
    }

    @Override
    public void appDynammicComment(DynamicCommentDto dynamicCommentDto) {
        AppEvaluationCommentInfo appEvaluationCommentInfo = new AppEvaluationCommentInfo();
        BeanUtils.copyProperties(dynamicCommentDto, appEvaluationCommentInfo);
        if (StringUtils.isEmpty(appEvaluationCommentInfo.getPlatform())) {
            appEvaluationCommentInfo.setPlatform("app");
        }
        if (StringUtils.isEmpty(appEvaluationCommentInfo.getChannel())) {
            appEvaluationCommentInfo.setChannel("default");
        }
        if (StringUtils.isEmpty(appEvaluationCommentInfo.getSeq_id())) {
            appEvaluationCommentInfo.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(appEvaluationCommentInfo.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            appEvaluationCommentInfo.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(appEvaluationCommentInfo);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.DYNAMIC_COMMENT_KAFKA_OUT_KEY);
        appEvaluationCommentInfo.setIncrementId(incrementId);
        appEvaluationCommentInfo.setCur_timestamp(System.currentTimeMillis());
        //发送到kafka
//        try{
//            log.info("appEvaluationComment={}", JSONObject.toJSONString(appEvaluationCommentInfo));
//            boolean sendStatus = kafkaSendService.sendDynammicComment(JSONObject.toJSONString(appEvaluationCommentInfo));
//        }catch (Exception e){
//            log.error("appEvaluationComment发送到kafka错误",e);
//        }
    }

    @Override
    public void appDynammicLike(DynamicLikeDto dynamicLikeDto) {
        AppDynamicLike appDynamicLike = new AppDynamicLike();
        BeanUtils.copyProperties(dynamicLikeDto, appDynamicLike);
        if (StringUtils.isEmpty(appDynamicLike.getSeq_id())) {
            appDynamicLike.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(appDynamicLike.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            appDynamicLike.setCur_date(sdf.format(new Date()));
        }
        mongoTemplate.save(appDynamicLike);

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.APP_DYNAMIC_LIKE_KEY);
        appDynamicLike.setIncrementId(incrementId);
        appDynamicLike.setCur_timestamp(System.currentTimeMillis());

        //发送到kafka
//        try{
//            log.info("appDynamicLike={}", JSONObject.toJSONString(appDynamicLike));
//            boolean sendStatus = kafkaSendService.sendDynamicLike(JSONObject.toJSONString(appDynamicLike));
//        }catch (Exception e){
//            log.error("appDynamicLike发送到kafka错误",e);
//        }
    }

    @Override
    public void appDynamicExposure(AppDataStatisticsExposureDto appDataStatisticsExposureDto) {
        String dynamicIds = appDataStatisticsExposureDto.getDynamicIds();
        String[] dynamicIds_array = dynamicIds.split(",");
        for (String dynamicId : dynamicIds_array) {
            AppDynamicExposure appDynamicExposure = new AppDynamicExposure();
            BeanUtils.copyProperties(appDataStatisticsExposureDto, appDynamicExposure);
            if (StringUtils.isEmpty(appDynamicExposure.getSeq_id())) {
                appDynamicExposure.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
            }
            if (StringUtils.isEmpty(appDynamicExposure.getCur_date())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                appDynamicExposure.setCur_date(sdf.format(new Date()));
            }
            appDynamicExposure.setDynamicId(dynamicId);
            mongoTemplate.save(appDynamicExposure);

            //获取自增主键
            Long incrementId = redisService.getIncrementId(RedisKeyConstant.APP_DYNAMIC_EXPOSURE_KEY);
            appDynamicExposure.setIncrementId(incrementId);
            appDynamicExposure.setCur_timestamp(System.currentTimeMillis());

            //发送到kafka
//            try{
//                log.info("appDynamicExposure={}", JSONObject.toJSONString(appDynamicExposure));
//                boolean sendStatus = kafkaSendService.sendDynamicExposure(JSONObject.toJSONString(appDynamicExposure));
//            }catch (Exception e){
//                log.error("appDynamicExposure发送到kafka错误",e);
//            }
        }

    }
}
